home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3929 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  64 lines

  1. Newsgroups: comp.lang.c++
  2. Path: usinternet.com!not-for-mail
  3. From: "Luke J. Bucklin" <lbucklin@mail.usinternet.com>
  4. Subject: Re: [] overload..(newbie in distress)
  5. Message-ID: <11a7cc$d2619.341@usinternet.com>
  6. Date: Fri, 26 Jan 1996 19:38:25 GMT
  7. Organization: Management Computer Solutions
  8. X-Mailer: Mozilla 1.22 (Windows; U; 16bit)
  9. MIME-Version: 1.0
  10. References: <DLGppJ.31C@undergrad.math.uwaterloo.ca> <4e0ui5$kjn@newsroom.hitc.com>
  11. Content-Transfer-Encoding: 7bit
  12. Content-Type: text/plain; charset=us-ascii
  13.  
  14.  
  15. psand@eos.hitc.com (G. Patrick Sand) wrote:
  16. >In article <DLGppJ.31C@undergrad.math.uwaterloo.ca>, 
  17. >tthiraku@landen.math.uwaterloo.ca says...
  18. >>
  19. >>Hi..
  20. >>
  21. >>I'm currently stuck on how to overload [] operator such that
  22. >>it does two different tasks..
  23. >>
  24. >>
  25. >>case 1:
  26. >>
  27. >>// A is an instance of a class that contains a linkist.
  28. >>
  29. >>   A[5] = val;  // store val into the fifth node of a linklist. 
  30. >>   val = A[5] ; // returns the value of the fifth node of a linklist.  
  31. >>
  32. >>
  33. >>I was wondering how can C++ differentiate these two senerios? 
  34. >>
  35. >>Anything help will greatly appreciated..
  36. >>
  37. >>Thank You Kindly
  38. >>-- 
  39. >>Thanou Thirakul
  40. >>tthiraku@undergrad.math.uwaterloo.ca
  41. >>http://www.undergrad.math.uwaterloo.ca/~tthiraku
  42.  
  43. Have the operator return a reference.  Then, since the reference is being 
  44. used, the = can be on either side:
  45.  
  46. Type &operator[](Valtype Val);
  47.  
  48. This should allow you to do
  49.  
  50. A[5] = val;
  51. as well as
  52. val = A[5]
  53.  
  54. because A[5] is actually a reference to the "return value".
  55.  
  56. ------------------------------------
  57. Luke J. Bucklin
  58. Management Computer Solutions, Inc.
  59. lbucklin@usinternet.com
  60. ------------------------------------
  61.  
  62.  
  63.  
  64.